package cn.shengchao.dubbo.api.wecom;

import cn.hutool.core.collection.CollUtil;
import cn.shengchao.examstar.rpc.wecom.api.ExternalInviteServiceApi;
import cn.shengchao.qywx.infrastrure.po.QywxExternalInvite;
import cn.shengchao.qywx.infrastrure.service.QywxExternalInviteService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.Date;
import java.util.List;

/**
 * @program: examstarapi
 * @description: 客户关联事件api
 * @author: Xiong weiqiang
 * @create: 2025-04-16 19:30
 */
@DubboService
@RequiredArgsConstructor
public class ExternalInviteServiceDubboApi implements ExternalInviteServiceApi {
    private final QywxExternalInviteService qywxExternalInviteService;
    @Override
    public void createExternalInvite(String userId, String externalUserId,String state) {
        QywxExternalInvite externalInvite = new QywxExternalInvite();
        externalInvite.setUserId(userId);
        externalInvite.setExternalUserId(externalUserId);
        externalInvite.setAddTime(new Date());
        externalInvite.setState(state);
        externalInvite.setAsDelete(0);
        qywxExternalInviteService.save(externalInvite);
    }

    @Override
    public void deleteExternalInvite(String userId, String externalUserId) {
        List<QywxExternalInvite> externalInviteList = qywxExternalInviteService.list(new LambdaQueryWrapper<>(QywxExternalInvite.class)
                .eq(QywxExternalInvite::getAsDelete, 0)
                .eq(QywxExternalInvite::getUserId, userId).eq(QywxExternalInvite::getExternalUserId, externalUserId));
        if (CollUtil.isNotEmpty(externalInviteList)) {
            externalInviteList.forEach(externalInvite -> {
                externalInvite.setAsDelete(1);
                externalInvite.setDeleteTime(new Date());
                qywxExternalInviteService.updateById(externalInvite);
            });
        }
    }
}